#!/usr/bin/env python
# -*- coding: utf-8 -*-
# @Time    : 2021/12/27 19:34
# @Author  : Sun
# @File    : get_family_token.py
# @Software: PyCharm
import json
import os.path
import time

import requests

from common.read_yaml import ReadYaml
from common.write_log import WriteLog


class GetFamilyToken:
    # _URL = ReadYaml().read_yaml().get('domain') + "/account/dailyActive"
    _URL = "http://192.168.9.17:22101/account/dailyActive"
    _DATA = {
        "JJUid": None,
        "JJToken": "test",
        "loginMethod": 1
    }
    _HEADERS = {
        'accept': 'application/json',
                  'platform': 'RN',
                  'Content-Type': 'application/json;charset=utf-8'
    }

    def get_token(self, JJUid):
        # WriteLog().write_log().info(JJUid)
        self._DATA["JJUid"] = JJUid
        # 添加等待，解决频繁调用Token返回为空的问题
        # time.sleep(3)
        # while True:
        #     time.sleep(3)
        r = requests.request(
            method='POST',
            url=self._URL,
            data=json.dumps(
                self._DATA),
            headers=self._HEADERS)
        WriteLog().write_log().info(r.json())
        # if 'data' in r.json():
        #     break
        token = r.json().get('data').get('token')
        # WriteLog().write_log().info(token)
        ReadYaml().write_yaml(file_path="./config/family_token.yaml", data={JJUid: token})
        return token


if __name__ == '__main__':
    GetFamilyToken().get_token(682139013)
